
/*
 * @项目名称: 模板开发
 * @FilePath: /meek/src/components/utils/intersect.js
 * @Author: kongxueyang@datatang.com
 * @Date: 2020-10-13T17:48:40+08:00
 * @LastEditors: yangxianghong@datatang.com
 * @LastEditTime: 2023-05-23 17:28:41
 * Copyright (c) 2018-2022
 */

import { polygon, booleanOverlap, booleanPointInPolygon, point, lineString, lineIntersect, intersect } from '@turf/turf';
import Geometry from './../../geometry/geometry';
export default function (feature1, feature2) {
  let obj1 = polygon([feature1.geometry.getCoordinates()]);
  let obj2 = null;
  if ([Geometry.EXTENT,
    Geometry.MULTI_POLYGON,
    Geometry.TILTRECTANGLE,
    Geometry.TRAPEZIUM,
    Geometry.POLYGON,
    Geometry.PARALLELOGRAM
  ].includes(feature2.geometry.geometryType)) {
      if (Geometry.EXTENT === feature2.geometry.geometryType) {
        obj2 = polygon([feature2.geometry.getCoordinates()]);
      } else {
        obj2 = polygon(feature2.geometry.getCoordinates());
      }
      return intersect(obj1, obj2);
  } else if (Geometry.POINT === feature2.geometry.geometryType) {
    return booleanPointInPolygon(point(feature2.geometry.getCoordinates()), obj1);
  } else if (Geometry.LINE === feature2.geometry.geometryType) {
    let coords1 = feature1.geometry.getCoordinates();
    let lineArray = [
      lineString([coords1[0], coords1[1]]),
      lineString([coords1[1], coords1[2]]),
      lineString([coords1[2], coords1[3]]),
      lineString([coords1[3], coords1[4]])
    ];
    let coords2 = feature2.geometry.getCoordinates();
    obj2 = lineString(coords2);
    return lineArray.some(line => {
      let c = lineIntersect(line, obj2);
      return c && c.features && c.features.length > 0;
    }) || (
      booleanPointInPolygon(point(coords2[0]), obj1) &&
      booleanPointInPolygon(point(coords2[coords2.length - 1]), obj1)
    )
  } else if ([Geometry.CIRCLE, Geometry.ELLIPSE, Geometry.CUBE].includes(feature2.geometry.geometryType)) {
    obj2 = polygon([feature2.geometry.extent.getCoordinates()]);
    console.log(obj2, 'obj2');
    return intersect(obj1, obj2);
  }
  return false;
}
